package com.departmentstore.service.impl;

import com.departmentstore.dto.ManufacturerProductDTO;
import com.departmentstore.entity.ManufacturerProduct;
import com.departmentstore.entity.ManufacturerProductId;
import com.departmentstore.entity.Manufacturer;
import com.departmentstore.entity.Product;
import com.departmentstore.repository.ManufacturerProductRepository;
import com.departmentstore.repository.ManufacturerRepository;
import com.departmentstore.repository.ProductRepository;
import com.departmentstore.service.ManufacturerProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ManufacturerProductServiceImpl implements ManufacturerProductService {

    private final ManufacturerProductRepository manufacturerProductRepository;
    private final ManufacturerRepository manufacturerRepository;
    private final ProductRepository productRepository;

    @Autowired
    public ManufacturerProductServiceImpl(
        ManufacturerProductRepository manufacturerProductRepository,
        ManufacturerRepository manufacturerRepository,
        ProductRepository productRepository
    ) {
        this.manufacturerProductRepository = manufacturerProductRepository;
        this.manufacturerRepository = manufacturerRepository;
        this.productRepository = productRepository;
    }

    @Override
    public ManufacturerProduct createManufacturerProduct(ManufacturerProduct manufacturerProduct) {
        ManufacturerProductId id = new ManufacturerProductId(
            manufacturerProduct.getManufacturerId(),
            manufacturerProduct.getProductId()
        );
        if (manufacturerProductRepository.existsById(id)) {
            throw new RuntimeException("该制造商-商品关联已存在");
        }

        // 获取制造商实体
        Manufacturer manufacturer = manufacturerRepository.findById(manufacturerProduct.getManufacturerId())
            .orElseThrow(() -> new RuntimeException("制造商不存在"));
        
        // 获取商品实体
        Product product = productRepository.findById(manufacturerProduct.getProductId())
            .orElseThrow(() -> new RuntimeException("商品不存在"));

        // 设置关联实体
        manufacturerProduct.setManufacturer(manufacturer);
        manufacturerProduct.setProduct(product);

        return manufacturerProductRepository.save(manufacturerProduct);
    }

    @Override
    public ManufacturerProduct updateManufacturerProduct(ManufacturerProduct manufacturerProduct) {
        ManufacturerProductId id = new ManufacturerProductId(
            manufacturerProduct.getManufacturerId(),
            manufacturerProduct.getProductId()
        );
        if (!manufacturerProductRepository.existsById(id)) {
            throw new RuntimeException("制造商产品关联不存在");
        }
        return manufacturerProductRepository.save(manufacturerProduct);
    }

    @Override
    public void deleteManufacturerProduct(String id) {
        String[] parts = id.split("_");
        if (parts.length != 2) {
            throw new RuntimeException("无效的ID格式");
        }
        ManufacturerProductId manufacturerProductId = new ManufacturerProductId(parts[0], parts[1]);
        if (!manufacturerProductRepository.existsById(manufacturerProductId)) {
            throw new RuntimeException("制造商产品关联不存在");
        }
        manufacturerProductRepository.deleteById(manufacturerProductId);
    }

    @Override
    @Transactional(readOnly = true)
    public ManufacturerProduct getManufacturerProductById(String id) {
        String[] parts = id.split("_");
        if (parts.length != 2) {
            throw new RuntimeException("无效的ID格式");
        }
        ManufacturerProductId manufacturerProductId = new ManufacturerProductId(parts[0], parts[1]);
        return manufacturerProductRepository.findById(manufacturerProductId)
                .orElseThrow(() -> new RuntimeException("制造商产品关联不存在"));
    }

    @Override
    @Transactional(readOnly = true)
    public List<ManufacturerProductDTO> getAllManufacturerProducts() {
        return manufacturerProductRepository.findAll().stream()
                .map(ManufacturerProductDTO :: new )
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public List<ManufacturerProduct> getManufacturerProductsByManufacturerId(String manufacturerId) {
        return manufacturerProductRepository.findByManufacturer(manufacturerId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ManufacturerProduct> getManufacturerProductsByProductId(String productId) {
        return manufacturerProductRepository.findByProduct(productId);
    }

    @Override
    @Transactional(readOnly = true)
    public ManufacturerProduct getManufacturerProductByManufacturerIdAndProductId(String manufacturerId, String productId) {
        return manufacturerProductRepository.findByManufacturerAndProduct(manufacturerId, productId);
    }
} 